import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_oldman_health/api/auth_service.dart';
import 'package:flutter_oldman_health/api/constant.dart';
import 'package:flutter_oldman_health/models/response_data_model.dart';
import 'package:flutter_oldman_health/models/user_model.dart';
import 'package:flutter_oldman_health/pages/setting/account_page.dart';
import 'package:flutter_oldman_health/pages/setting/bind_phone_page.dart';
import 'package:flutter_oldman_health/pages/setting/change_psw_page.dart';
import 'package:flutter_oldman_health/utils/family_add_assist.dart';
import 'package:flutter_oldman_health/utils/utils.dart';
import 'package:flutter_oldman_health/widget/common_container.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'login_page.dart';

class UserPager extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _UserPagerState();
}

class _UserPagerState extends State<UserPager> {
  UserInfo userInfo;

  void queryUserInfo() {
    if (userInfo != null) {
      return;
    }
    AuthService.queryUserInfo().then((userData) {
      if (null != userData && userData.statusCode == 200) {
        UserModel userModel = UserModel.fromJson(userData.data);
        if (null != userModel && userModel.success()) {
          setState(() {
            userInfo = userModel.data;
          });
        }
      }
    });
  }

  @override
  void dispose() {
    FamilyAddAssist().offEventBus();
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
    queryUserInfo();
    FamilyAddAssist().setEventBus(context);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        color: Constant.backgroundColor,
        padding: EdgeInsets.all(16),
        child: Column(
          children: <Widget>[
            Padding(
                padding: EdgeInsets.fromLTRB(0, 35, 0, 10),
                child: Text("个人中心",
                    style: TextStyle(fontSize: 22, color: Colors.black87))),
            CommonContainer.item(context, "账号", "images/ic_account.png",
                AccountPage(userInfo: userInfo), 24),
            CommonContainer.item(context, "手机号换绑",
                "images/ic_account_phone.png", BindPhonePage(), 24),
            CommonContainer.item(context, "密码修改",
                "images/ic_account_change_pw.png", ChangePswPage(), 24),
            // CommonContainer.item(context, "APP分享", "images/ic_account.png",
            //     ChangePswPage(), 24),
            // CommonContainer.item(context, "版本升级", "images/ic_account.png",
            //     ChangePswPage(), 24),
            Container(
                margin: EdgeInsets.only(top: 26),
                padding: EdgeInsets.all(12),
                child: GestureDetector(
                    behavior: HitTestBehavior.translucent,
                    onTap: () async {
                      var response = await AuthService.logout();
                      if (null != response && response.statusCode == 200) {
                        ResponseDataModel<int> value =
                            ResponseDataModel<int>.fromJson(response.data);
                        if (value.success()) {
                          final prefs = await SharedPreferences.getInstance();
                          prefs.remove("password");

                          prefs.remove("token");
                          prefs.remove("userId");
                          prefs.remove("user_nick");
                          prefs.remove("user_mobile");

                          Navigator.pushReplacement(context,
                              MaterialPageRoute(builder: (context) => LoginPage()));
                        } else {
                          EasyLoading.showInfo(value.msg);
                        }
                      } else {
                        print(response);
                        // EasyLoading.showInfo("请求服务器失败，请检查网络");
                      }
                    },
                    child: FractionallySizedBox(
                        widthFactor: 1,
                        child: Text("退出账号",
                            textAlign: TextAlign.center,
                            style: TextStyle(
                                fontWeight: FontWeight.bold,
                                fontSize: 19,
                                color: Color(0xff41BAB0))))),
                decoration: BoxDecoration(
                    border: Border.all(color: Color(0xFFEEEEEE), width: 1),
                    color: Colors.white,
                    borderRadius: BorderRadius.all(Radius.circular(38))))
          ],
        ));
  }
}
